use std::cmp;

struct Solution;

impl Solution {
    pub fn max_product_dp(nums: Vec<i32>) -> i32 {
        let mut result = i32::MIN;
        let mut imax = 1;
        let mut imin = 1;
        for i in nums {
            if i < 0 {
                std::mem::swap(&mut imax, &mut imin);
            }
            imax = std::cmp::max(i, imax * i);
            imin = std::cmp::min(i, imin * i);
            result = std::cmp::max(result, imax);
        }
        result
    }

    pub fn max_product_note(nums: Vec<i32>) -> i32 {
        if nums.len() == 1 {
            return nums[0];
        }
        let mut map = nums.clone();
        let mut result: i32 = *nums.iter().max().unwrap();
        for offset in 1..nums.len() {
            for i in 0..(nums.len() - offset) {
                map[i] *= nums[i + offset];
                result = std::cmp::max(map[i], result);
            }
        }
        result
    }

    pub fn max_product(nums: Vec<i32>) -> i32 {
        if nums.len() == 1 {
            return nums[0];
        }
        let mut result = 0;
        for len in 1..=nums.len() {
            for i in 0..=(nums.len() - len) {
                let mut tmp = 1;
                for j in i..(i + len) {
                    tmp *= nums[j];
                }
                result = cmp::max(tmp, result);
            }
        }
        result
    }
}

fn main() {
    println!(
        "max is {}",
        Solution::max_product_dp(vec![1, 2, 3, 0, 4, 5, -1])
    );
}
